package pl.edu.pw.stud.ltrzaska.validator;

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.log4j.Logger;

import pl.edu.pw.stud.ltrzaska.logic.ValidationContext;
import pl.edu.pw.stud.ltrzaska.policy.entity.Directory;
import pl.edu.pw.stud.ltrzaska.policy.entity.File;
import pl.edu.pw.stud.ltrzaska.policy.entity.FilesystemObject;
import pl.edu.pw.stud.ltrzaska.validator.iface.IFilesystemObjectValidator;

public class NameValidator implements IFilesystemObjectValidator {
	private final static Logger LOGGER = Logger.getLogger(NameValidator.class);
	
	// TODO write specific existence validation for Group and Sequence
	@Override
	public boolean validate(FilesystemObject policyFsObj, FileObject virtualFsObj, ValidationContext vc)
			throws FileSystemException {
		LOGGER.debug(".validate() for FilesystemObject");		
		FileObject[] fos = virtualFsObj.getParent().getChildren();
		boolean isFound = false;
		for (FileObject fo : fos) {
			String n = fo.getName().getBaseName();
			if (n.equals(policyFsObj.getName())) {
				isFound = true;
				break;
			}
		}
		boolean result = false;
		if (null == policyFsObj.getExistence() || "".equals(policyFsObj.getExistence())) {
			policyFsObj.setExistence(FilesystemObject.EXISTENCE_DEFAULT);
		}
		if (FilesystemObject.EXISTENCE_OBLIGATORY.equals(policyFsObj.getExistence())) {
			if (isFound) {
				result = true;
			}
		}
		if (FilesystemObject.EXISTENCE_FORBIDDEN.equals(policyFsObj.getExistence())) {
			if (isFound) {
				result = false;
			}
		}
		if (FilesystemObject.EXISTENCE_OPTIONAL.equals(policyFsObj.getExistence())) {
			result = true;
		}

		return result;
	}
	public boolean validate(File f, FileObject parentVirtualFsObj) {
		LOGGER.debug(".validate() for File");
		return true;
	}
	public boolean validate(Directory d, FileObject parentVirtualFsObj) {
		LOGGER.debug(".validate() for Dir");
		return true;
	}
	@Override
	public boolean canValidate(FilesystemObject pfo, FileObject vfo) {
		// TODO Auto-generated method stub
		return false;
	}
}
